package org.app.main;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.app.domain.Car;
import org.app.domain.Contact;
import org.app.service.IContactService;
import org.hibernate.Hibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes(value = "contact")
public class ContactController extends BaseController {
	private static final Logger logger = LoggerFactory
			.getLogger(ContactController.class);

	@Autowired
	private IContactService contactService;

	@RequestMapping(value = "/contact/list", method = RequestMethod.GET)
	public String listContacts(Map<String, Object> map, HttpServletRequest req) {
		logger.info("in contact web");
		logger.info(getValue(req));
		// map.put("contact", new Contact());
		map.put("contactList", contactService.listContact());
		
		List<Contact> listContact = contactService.listContact();
/*		for (Contact c : listContact) {
			logger.info(c.getFirstName());
			
				logger.info("size is "+c.getCars().size());
				Set<Car> cars = c.getCars();
				Iterator iter = cars.iterator();
				while (iter.hasNext()) {
					Car car = (Car) iter.next();
					logger.info(car.getModel());
					logger.info(car.getMake());
			}

		}*/
		logger.info("results size" + map.size());
		return "contact/list";
	}

	@RequestMapping(value = "/contact/add", method = RequestMethod.GET)
	public String addContact(@ModelAttribute("newContact") Contact contact) {
		return "contact/add";
	}

	@RequestMapping(value = "/contact/add", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("newContact") Contact contact,
			BindingResult result) {

		contactService.addContact(contact);
		return "redirect:/contact/list";
	}

	@RequestMapping(value = "/contact/delete/{contactId}")
	public String deleteContact(@PathVariable("contactId") Long contactId) {

		contactService.removeContact(contactId);

		return "redirect:/contact/list";
	}

	@RequestMapping(value = "/contact/edit/{contactId}", method = RequestMethod.GET)
	public String editContact(ModelMap model,
			@PathVariable("contactId") Long contactId) {
		logger.info("in edit:");
		Contact temp = contactService.selectContactById(contactId);
		model.addAttribute("contact", temp);

		logger.info("contact id:" + temp.getId());
		logger.info(temp.toString());
		return "contact/edit";
	}

	@RequestMapping(value = "/contact/edit/{contactId}", method = RequestMethod.POST)
	public String editContactPost(@ModelAttribute("contact") Contact contact,
			BindingResult result, @PathVariable("contactId") Long contactId) {
		logger.info("in post");
		// logger.info("firstname" + contact.getFirstName());
		logger.info("contactId" + contactId);
		logger.info("contact id:" + contact.getId());
		logger.info(contact.toString());
		contactService.updateContact(contact);
		return "redirect:/contact/list";
	}

}
